﻿using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
using System.Drawing;
//using System.Linq;
//using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;

namespace TinyScreenShoot
{
    public class ScreenShoot : Form
    {
        private Image theImage;
        private Rectangle mRect;
        private Point imgStart;
        private Size imgSize;

        public ScreenShoot()
        {
            //InitializeComponent();
            this.TopMost = true;
            this.DoubleBuffered = true;
            this.ShowInTaskbar = false;
            this.FormBorderStyle = FormBorderStyle.None;//hide max, min button
            this.WindowState = FormWindowState.Maximized;//make it full
            //this.BackColor = Color.Purple;
            //this.TransparencyKey = Color.Purple;
            //SetStyle(ControlStyles.Opaque, true);

            //capture the whole screen
            Rectangle bounds = Screen.GetBounds(Point.Empty);
            using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
            {
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
                }
                theImage = (Image)bitmap.Clone();
            }
        }

        //Initiate rectangle with mouse down event
        protected override void OnMouseDown(MouseEventArgs e)
        {
            mRect = new Rectangle(e.X, e.Y, 0, 0);
            imgStart.X = (int)e.X;
            imgStart.Y = (int)e.Y;
            this.Invalidate();
        }

        //get x, y of mouse when it is up
        protected override void OnMouseUp(MouseEventArgs e)
        {
            //calculate image shot size
            imgSize.Width = (int) Math.Abs(e.X - imgStart.X);
            imgSize.Height = (int) Math.Abs(e.Y - imgStart.Y);

            if (imgSize.Width + imgSize.Height != 0)
            {//shot
                using (Bitmap bitmap = new Bitmap(imgSize.Width, imgSize.Height))
                {
                    using (Graphics g = Graphics.FromImage(bitmap))
                    {
                        //just copy a rectangle that upper-left vertex is imgStart, its size is imgSize
                        g.CopyFromScreen(imgStart.X, imgStart.Y, 0, 0, imgSize);
                    }

                    //save the screenshot as ss.jpg, ss0.jpg, ss1.jpg...
                    String desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                    String fullFilePath = Path.Combine(desktopFolder, "ss.jpg");
                    int i = 0;

                    while (File.Exists(fullFilePath))
                    {
                        fullFilePath = Path.Combine(desktopFolder, "ss" + i.ToString() + ".jpg");
                        i++;
                    }
                    bitmap.Save(fullFilePath, ImageFormat.Jpeg);
                }
            }
            //quit
            Application.Exit();

        }

        //check if mouse is down and being draged, then draw rectangle
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);
                this.Invalidate();
            }
        }

        //draw the rectangle on paint event
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.DrawImage(theImage, ClientRectangle);
            //Draw a rectangle with 2pixel wide line
            using (Pen pen = new Pen(Color.Black, 1))
            {
                e.Graphics.DrawRectangle(pen, mRect);
            }

        }
    }
}
